<script setup lang='ts'>
import { ArrowRight } from '@element-plus/icons-vue'
import { onMounted, ref, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import titlepc from "../../components/pc/titlepc.vue";
import contentpc from "../../components/pc/contentpc.vue";
import footerVue from "../../components/pc/footerVue.vue";
import { Liveinitialization,download } from "../../api/api";
import { ElNotification } from 'element-plus'
import {
    getDetailsMaterial,
    getRandomDetailsMaterial,
    collectinitialization,
    Live,
    collect,
    userLogin
} from "../../api/api";
const router = useRouter()
const route = useRoute()
const frequency = ref()
const data = ref<any>([])
const isloginData = ref()
const randData = ref()
const colorL = ref('#999')
const colorC = ref('#999')
const srcList = ref<any>([])
onMounted(() => {
    getData()
    randomData()
    isLike()
    isstar()
    ifLogin()
})
const onBack = () => {
    router.back()
}
//获取数据
const getData = async () => {
    const res = await getDetailsMaterial({ _id: route.query.type as string })
    data.value.push(res.data.data)
    srcList.value.push(res.data.data.imgdomainUrl)
}
//随机4条数据
const randomData = async () => {
    const res = await getRandomDetailsMaterial()
    randData.value = res.data.data
}
//监视id
watch(() => route.query.type, (value) => {
    data.value = []
    if (value) {
        getData()
        randomData()
    }

})
// 初始化点赞
const isLike = async () => {
    const res = await Liveinitialization({ mail: localStorage.getItem('mail') as string, id: route.query.type as string })
    if (res.data.code === '200') return colorL.value = 'red'
    colorL.value = '#999'
}
// 初始化收藏
const isstar = async () => {
    const res = await collectinitialization({ mail: localStorage.getItem('mail') as string, id: route.query.type as string })
    if (res.data.code === '200') return colorC.value = 'yellow'
    colorC.value = '#999'
}
//点赞与取消
const like = async (e: string) => {
    const res = await Live({ mail: localStorage.getItem('mail') as string, id: e })
    if (res.data.code === '404') return open1('点赞失败','没登录或登录过期','error')
    if (res.data.code === '200') return colorL.value = 'red'
    colorL.value = '#999'
}
//收藏与取消
const star = async (e: string) => {
    const res = await collect({ mail: localStorage.getItem('mail') as string, id: e })
    if (res.data.code === '404') return open1('收藏失败','没登录或登录过期','error')
    if (res.data.code === '200') return colorC.value = 'yellow'
    colorC.value = '#999'
}
//判断是否登录
const ifLogin = async () => {
    const res = await userLogin({ _id: localStorage.getItem('mail') as string })
    if (res.data.code === '404') {
        localStorage.setItem('token', '')
        localStorage.setItem('mail', '')
        colorL.value = '#999'
        colorC.value = '#999'
        isloginData.value = false
        return
    }
    isloginData.value = true
    frequency.value = res.data.data.frequency
}
//状态
const open1 = (title:string,text:string,types:any) => {
  ElNotification({
    title: title,
    message: text,
    type: types,
  })
}
//下载
const onClickButton = async (_id: string) => {
    const res = await download({ id: _id, mail: localStorage.getItem('mail') as string })
    if (res.data.type === 'text/html') return open1('下载失败','积分不够，请充值积分','error')
    if (res.data.type === 'application/json') {
        localStorage.setItem('token', '')
        localStorage.setItem('mail', '')
        ifLogin()
        open1('下载失败','没登录或登录过期','error')
        return
    }
    ifLogin()
    const url = URL.createObjectURL(res.data)
    const a = document.createElement('a')
    a.href = url
    a.style.display = 'none'
    a.download = '素材'
    a.click()
    URL.revokeObjectURL(a.href)
    a.remove()
    open1('下载成功','请等待下载完成','success')
};
</script>

<template>
    <div class="color">
        <div class="headerpc">
            <div class="title">
                <el-page-header title="返回" @back="onBack">
                    <template #content>
                        <div class="flex items-center">
                            <img src="../../assets/t.png" style="width: 20%;margin-left: -50px" alt="">
                            <el-tag class="tag" v-if="isloginData">积分：{{frequency}}</el-tag>
                            <el-tag class="tag" type="danger" v-if="!isloginData">未登录</el-tag>
                        </div>
                    </template>
                </el-page-header>
            </div>
        </div>
        <div class="h"></div>
        <div class="crumb">
            <el-breadcrumb :separator-icon="ArrowRight">
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>素材库</el-breadcrumb-item>
                <el-breadcrumb-item v-for="i in data">{{ i.name }}</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="data" v-for="i in data">
            <div class="img">
                <el-image style="width: 350px; height: 450px;"
                    :src="i.imgdomainUrl" fit="cover"
                    :preview-src-list="srcList" />
            </div>
            <div class="Ddata">
                <div class="title">
                    <h2>素材详情</h2>
                </div>
                <div class="xq">
                    <p>素材id：<span>{{ i._id }}</span></p>
                    <p>素材名称：<span>{{ i.name }}</span></p>
                    <p>素材分类：<span>{{ i.selectData }}</span></p>
                    <p>素材积分：<span>{{ i.types }}</span></p>
                    <p>素材大小：<span>{{ i.size }}</span></p>
                    <p>文件格式：<span>fla</span></p>
                </div>
                <div class="text">
                    沙雕动画素材网是一家专注于沙雕动画制作素材的免费下载的图库作品网站，当前资源有沙雕人物素材、沙雕场景素材、沙雕特效教程、音效教材，还有制作教程，是制作沙雕动画一条龙的专业网站。
                </div>
                <div class="btnData">
                    <div class="btn" @click="onClickButton(i._id)">
                        <h3>下载素材</h3>
                    </div>
                    <van-action-bar-icon class="icon" icon="like" text="点赞" :color="colorL" @click="like(i._id)" />
                    <van-action-bar-icon class="icon" icon="star" text="收藏" :color="colorC" @click="star(i._id)" />
                </div>
            </div>
        </div>
        <titlepc :num="-480" title="随机推荐素材"></titlepc>
        <contentpc :data="randData" :isTrue="false"></contentpc>
    </div>
    <footerVue></footerVue>
</template>

<style scoped>
.color {
    background-color: #f5f2f0;
    min-height: 100vh !important;
}

.flex {
    display: flex;
    justify-content: center;
    align-items: center;
}

.text-large,
.mr-2,
.tag {
    position: relative;
    left: 5px;
}

.tag {
    left: 15px;
}

.headerpc {
    width: 100%;
    height: 60px;
    position: fixed;
    top: 0;
    background-color: rgba(255, 255, 255, 0.904);
    filter: blur(0.3px);
    z-index: 1;
}

.title {
    max-width: 1500px;
    height: 60px;
    box-sizing: border-box;
    margin: auto;
    padding: 10px;
}

.crumb {
    max-width: 1500px;
    margin: auto;
    margin-top: 60px;
}

.h {
    height: 20px
}

.data {
    max-width: 1500px;
    min-height: 600px;

    margin: auto;
    margin-top: 20px;
    display: grid;
    grid-template-columns: 73% 25%;
    gap: 2%;
}

.img {
    background-color: white;
    box-sizing: border-box;
    padding: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.Ddata {
    background-color: white;
    box-sizing: border-box;
    padding: 20px;
}

.title {
    width: 100%;
    text-align: center;
}

.xq {
    width: 90%;
    min-height: 10px;
    margin: auto;
}

p {
    margin-top: 10px;
}

.text {
    width: 90%;
    color: white;
    min-height: 50px;
    margin: auto;
    margin-top: 30px;
    border-radius: 10px;
    background-color: #999;
    box-sizing: border-box;
    padding: 20px
}

.icon {
    width: 20px;
}

.btnData {
    width: 90%;
    height: 40px;
    margin: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 30px;
}

.btn {
    width: 62%;
    height: 40px;
    background-color: #67c23a;
    color: white;
    display: flex;
    border-radius: 5px;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: 0.8s background-color ease, 0.8s color ease;
}

.btn:active {
    box-shadow: 0 0 5px 5px #ccc;
}

.btn:hover {
    background-color: rgba(255, 0, 0, 0.815);
    color: whitesmoke;
}

span {
    color: black;
}

p {
    color: #999;
}
</style>